#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    bool validateStackSequences(vector<int>& pushed, vector<int>& popped) {
        stack<int> st;
        int n = 0, i = 0;
        while (n < pushed.size())
        {
            st.push(pushed[n++]);
            while (st.size() && i < popped.size() && st.top() == popped[i]) st.pop(), i++;
        }
        return i == popped.size();
    }
};